package com.steven.treatment_nurse.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.base.BaseActivity;
import com.steven.treatment_nurse.breadCast.CallReceiver;
import com.steven.treatment_nurse.entity.ResultEntity;
import com.steven.treatment_nurse.net.MyCallback;
import com.steven.treatment_nurse.net.TaskNoHead;
import com.steven.treatment_nurse.utils.KeyboardUtils;
import com.steven.treatment_nurse.utils.MacUtils;
import com.steven.treatment_nurse.utils.SPUtils;
import com.steven.treatment_nurse.utils.SystemUtils;
import com.steven.treatment_nurse.utils.ToastUtils;

import retrofit2.Response;

public class LoginActivity extends BaseActivity implements KeyboardUtils.OnSoftKeyboardChangeListener {
    private EditText editUser;
    private EditText editPwd;
    private RelativeLayout rel_load;
    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        mOnGlobalLayoutListener = KeyboardUtils.observeSoftKeyboard(this, this);
        editUser = findViewById(R.id.edit_user);
        editPwd = findViewById(R.id.edit_pwd);
        rel_load = findViewById(R.id.rel_load);
        Button btn_login;
        (btn_login = (Button) findViewById(R.id.btn_login)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }


    //登录
    public void loginIM() {
        String userName = MacUtils.getMac(this).replace(":","-");
        String pwd ="123456";
        EMClient.getInstance().login(userName, pwd, new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                Log.e("main", "登录聊天服务器成功！");
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.e("main", message + "==" + code);
            }
        });
    }


    //登录
    private void login() {
        rel_load.setVisibility(View.VISIBLE);
        String userName = editUser.getText().toString();
        String userPwd = editPwd.getText().toString();
        if (userName.equals("") || userPwd.equals("")) {
            rel_load.setVisibility(View.GONE);
            ToastUtils.showToast(LoginActivity.this, "请输入用户名或密码~");
            return;
        }
        TaskNoHead.getInstance().login(userName, userPwd).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response response) {
                try {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(response.body());
                    assert jsonObject != null;
                    if (jsonObject.getInteger("code") == 0) {
                        loginIM();
                        rel_load.setVisibility(View.GONE);
                        JSONArray jsonArray=jsonObject.getJSONArray("data");
                        String token=jsonArray.getJSONObject(1).getString("token");
                        SPUtils.put(LoginActivity.this,"token",token);
                        Intent it=new Intent(LoginActivity.this, HSBedOverActivity.class);
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(it);
                        finish();
                    }else{
                        rel_load.setVisibility(View.GONE);
                        ToastUtils.showToast(LoginActivity.this,jsonObject.getString("msg"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    rel_load.setVisibility(View.GONE);
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                rel_load.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onSoftKeyBoardChange(int softKeyboardHeight, boolean visible) {
        if (!visible) {
            SystemUtils.hideBottomMenu(LoginActivity.this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KeyboardUtils.removeSoftKeyboardObserver(this, mOnGlobalLayoutListener);
    }

}
